export default {
  data() {
    return {
      debounceFlag: true,
      throttleFlag: true,
    };
  },
  methods: {
    debounce(fn, wait = 500, isImmediate = false) {
      var context = this;
      var args = arguments;
      clearTimeout(timerId);
      if (isImmediate) {
        if (this.debounceFlag) {
          fn.apply(context, args);
          this.debounceFlag = false;
        }
        timerId = setTimeout(function() {
          this.debounceFlag = true;
        }, wait);
      } else {
        timerId = setTimeout(function() {
          fn.apply(context, args);
        }, wait);
      }
    },
    throttle(fn, wait = 500, isImmediate = false) {
      if (this.throttleFlag === true) {
        var context = this;
        var args = arguments;
        this.throttleFlag = false;
        isImmediate && fn.apply(context, args);
        setTimeout(() => {
          !isImmediate && fn.apply(context, args);
          this.throttleFlag = true;
        }, wait);
      }
    },
  },
};
